//
//  Env.swift
//  Social
//
//  Created by 李响 on 2019/5/14.
//  Copyright © 2019 shengsheng. All rights reserved.
//

import Foundation
import WebKit
import Kingfisher

enum Env: Int, CaseIterable {
    case main = 1  // 生产
    case beta = 2  // 测试环境
    case code = 3  // 开发环境
    #if DEBUG
    case mock = 4  // 本地
    #endif
    
    /// 通过 JIECAO WIFI Mac 地址判断开启 Debug 模式。
    static var isDebug: Bool {
        #if targetEnvironment(simulator)
        return true
        #else
        return UIDevice.current.ssid == "JIECAO"
        #endif
    }
}

extension Env {
    
    /// 当前环境
    static var current: Env {
        get {
            let raw = UserDefaults.DebugInfo.integer(forKey: .mode)
            #if DEBUG
            return Env(rawValue: raw) ?? .main
            #else
            return Env(rawValue: raw) ?? .main
            #endif
        }
        set {
            UserDefaults.DebugInfo.set(newValue.rawValue, forKey: .mode)
            UserDefaults.standard.synchronize()
        }
    }
}

extension Env {
    
    var name: String {
        switch self {
        case .main:     return "PRODUCTION"
        case .beta:     return "BETA"
        case .code:     return "CODE"
        #if DEBUG
        case .mock:     return "MOCK"
        #endif
        }
    }
}

extension Env {
    
    /// API Host
    var configHost: String {
        switch self {
        case .main:     return "https://conf.realdating.cn"
        case .beta:     return "http://conf-test.realdating.cn"
        case .code:     return "http://conf-dev.realdating.cn"
        #if DEBUG
        case .mock:     return "http://mock.qiangshouapp.com:7300/mock/5cdaa3e106960021c647d991/1.0.0"
        #endif
        }
    }
    
    var commonHost: String {
        switch self {
        case .main:     return "https://app2.realdating.cn"
        case .beta:     return "http://app-test.realdating.cn"
        case .code:     return "http://app-dev.realdating.cn"
        #if DEBUG
        case .mock:     return "http://mock.qiangshouapp.com:7300/mock/5cdaa3e106960021c647d991/1.0.0"
        #endif
        }
    }
    
    /// 神策统计服务
    var sensorsServer: String {
        switch self {
        case .main:     return "https://arrivethetop.datasink.sensorsdata.cn/sa?project=production&token=c73b8f79da32e960"
        case .beta:     return "https://arrivethetop.datasink.sensorsdata.cn/sa?project=default&token=c73b8f79da32e960"
        case .code:     return "https://arrivethetop.datasink.sensorsdata.cn/sa?project=default&token=c73b8f79da32e960"
        #if DEBUG
        case .mock:     return "https://arrivethetop.datasink.sensorsdata.cn/sa?project=default&token=c73b8f79da32e960"
        #endif
        }
    }
    
    /// 推送
    var pushKey: String {
        switch self {
        case .main:    return "7b3bb0f40e743d65374d3833"
        case .beta:    return "7b3bb0f40e743d65374d3833"
        case .code:    return "9dd9894aab4424d747eb1186"
        #if DEBUG
        case .mock:    return "9dd9894aab4424d747eb1186"
        #endif
        }
    }
    
    /// 环信
    var easeMobKey: String {
        switch self {
        case .main:    return "1113190618090767#real"
        case .beta:    return "1113190618090767#real"
        case .code:    return "1113190618090767#real"
        #if DEBUG
        case .mock:    return "1113190618090767#real"
        #endif
        }
    }
    
    /// 应用版本
    static var version: String {
        return UIApplication.shared.semanticVersion ?? "0.0.0"
    }
}

extension Env {
    
    static func show(_ controller: UIViewController) {
        guard controller.presentedViewController == nil else { return }
//        let vc = DeveloperController.instance()
//        controller.present(vc, animated: true, completion: nil)
    }
}

extension UserDefaults {
    
    enum DebugInfo: UserDefaultsSettable {
        enum defaultKeys: String {
            case mode
        }
    }
}
